from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
# 导入类视图

from django.views import View

# from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from mydjango.settings import BASE_DIR

# 导包
from django.db.models import Q, F

# 导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid
from myapp.models import User, Pics
import redis
import base64
from hashlib import sha256
import hmac
import urllib
import re
from myapp.myser import UserSer, PicsSer

# IP和端口

host = 'localhost'
port = 6379

# host = '192.168.99.100'
# port = 6380

# 链接对象
r = redis.Redis(host=host, port=port)

from qiniu import Auth
from mydjango.settings import SECRET_KEY
from django.utils.deprecation import MiddlewareMixin
from django.utils.decorators import method_decorator


class UpdPicsView(APIView):  # 修改活动图片
    def get(self, request):
        id = request.GET.get('id')
        pic = Pics.objects.filter(id=id).first()
        pic_ser = PicsSer(pic)
        return Response(pic_ser.data)

    def post(self, request):
        id = request.POST.get('id')
        title = request.POST.get('title')
        link = request.POST.get('link')
        img = request.FILES.get('img')
        print(id, title, link, img)
        if img:
            Pics.objects.filter(id=id).update(title=title, link=link, image=img)
            return Response({
                'code': 200,
                'data': '修改成功'
            })
        else:
            Pics.objects.filter(id=id).update(title=title, link=link)
            return Response({
                'code': 200,
                'data': '修改成功,未修改图片'
            })


class InserPicsView(APIView):  # 添加幻灯片图片
    def get(self, request):  # 幻灯片获取
        pics = Pics.objects.all()
        pic_ser = PicsSer(instance=pics, many=True)
        return Response(pic_ser.data)

    def post(self, request):  # 添加幻灯片
        title = request.POST.get('title')
        link = request.POST.get('link')
        img = request.FILES.get('img')
        if not img:
            return Response({
                'code': 405,
                'data': '没有上传活动那个图片'
            })
        pics = Pics.objects.filter(title=title).first()
        if pics:
            return Response({
                'code': 405,
                'data': '该活动已存在'
            })
        Pics.objects.create(title=title, link=link, image='http://127.0.0.1:8000/static/upload/' + img.name)

        f = open(os.path.join(UPLOAD_ROOT, '', img.name), 'wb')  # 保存本地图片
        for chunk in img.chunks():
            f.write(chunk)
        f.close()

        return Response({
            'code': 200,
            'data': '添加成功'
        })

    def delete(self, request):  # 删除幻灯片
        id = request.GET.get('id')
        pics = Pics.objects.filter(id=id).delete()
        if pics:
            return Response({
                'code': 200,
                'data': '删除成功'
            })
        else:
            return Response({
                'code': 500,
                'data': '删除失败'
            })
